﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using AnyOffice.DAL;
using AnyOffice.Components;
using AnyOffice.Appcode;

namespace AnyOffice.Web.general.system.netshareMagerGX
{
    public partial class folader_create : BasePage
    {

        public AnyOffice.Model.DiskGX d=null;
        public DiskGXDao fold = new DiskGXDao();
        DiskTypeGXDao dtbll = new DiskTypeGXDao();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (!string.IsNullOrEmpty(Request.QueryString["DiskID"]) && !string.IsNullOrEmpty(Request.QueryString["op"]))
                {
                    hdd_diskid.Value = Request.QueryString["DiskID"].ToString();
                    d = fold.GetModel(base.Conn, Convert.ToInt32(hdd_diskid.Value));
                    Initfold();
                    spa_title.InnerText = "文件(夹)修改";
                }

                if (Session["pid"] != null)
                {
                    labl_path.Text = fold.GetParentPath(base.Conn, Session["pid"].ToString(), "");
                }
               
            }
        }
         /// <summary>
        /// 初始化数据
        /// </summary>
        protected void Initfold() 
        {
            labl_path.Text = d.ParentDiskID.ToString();
            txt_name.Text = d.Name;
            txt_key.Text = d.Keyword;
        }

        /// <summary>
        /// 保存操作
        /// </summary>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (hdd_diskid.Value != "")
            {
                if (!updatefold()) {
                   MessageBox.Show(this, "该目录已存在，无法创建！");
                }
            }
            else 
            {
                if (!Addfold())
                {
                    MessageBox.Show(this,"该目录已存在，无法创建！");
                }
            }
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script language = javascript>parent.menu_main.location.href='Public_folder.aspx?pid=" +
                Session["pid"].ToString() + "'</script>");
        }


        /// <summary>
        /// 添加文件夹
        /// </summary>
        protected bool Addfold() 
        {
            AnyOffice.Model.DiskGX foldmodel = new  AnyOffice.Model.DiskGX();
            string path = "";
            long pareid=0;
            if (Session["pid"] != null)
            {
                pareid= Convert.ToInt64(Session["pid"].ToString());;
                foldmodel.ParentDiskID =pareid;
                foldmodel.DiskTypeID = fold.GetModel(base.Conn, pareid).DiskTypeID;
                path = fold.GetParentPath(base.Conn, pareid.ToString(), "");
                path = Public_foldGX.GetPath(path,"",foldmodel.DiskTypeID.ToString());
            }
            foldmodel.UserID = Session["UserID"].ToString();
            foldmodel.FileTypeID = 1;
            foldmodel.Name = txt_name.Text.Trim().ToString();
            foldmodel.Keyword = txt_key.Text.Trim().ToString();
            foldmodel.UpdateTime = DateTime.Parse(DateTime.Now.ToShortDateString());
            foldmodel.FileSize = 0;
            path +="\\" + txt_name.Text.Trim();
            if (CreateFolder(path))//如果物理路径添加成功，写入一条数据
            {
                fold.Add(base.Conn, foldmodel);
                return true;
            }
            return false;
        }

        /// <summary>
        /// 编辑文件/夹
        /// </summary>
        protected bool updatefold() 
        {
            try
            {
                d = fold.GetModel(base.Conn, Convert.ToInt32(hdd_diskid.Value));//修改对象
                if (d.Name == txt_name.Text.Trim().ToString())//名称是否修改
                {
                    d.Keyword = txt_key.Text.Trim().ToString();
                    fold.Update(base.Conn, d);//更新
                    return true;
                }
                else
                {
                    string oldname = fold.GetParentPath(base.Conn, d.DiskID.ToString(), "");//获得目录路径
                    oldname = Public_foldGX.GetPath(oldname, "", d.DiskTypeID.ToString());//获得盘符目录路径
                    string newname = oldname.Replace(d.Name, txt_name.Text.Trim());
                    if (Public_foldGX.RemoveFiles(oldname, newname, d.FileTypeID.ToString()))//修改物理文件名
                    {
                        d.Keyword = txt_key.Text.Trim().ToString();
                        d.Name = txt_name.Text.Trim().ToString();
                        fold.Update(base.Conn, d);//更新名称
                        return true;
                    }
                    else { return false; }
                }
            }catch
            {
                return false;
            }
        }

        /// <summary>
        /// 创建物理文件夹
        /// </summary>
        /// <param name="FolderPathName"></param>
        protected bool CreateFolder(string FolderPathName)
        {
            if (FolderPathName.Trim().Length > 0)
            {
                try
                {
                    if (!Directory.Exists(FolderPathName))
                    {
                        Directory.CreateDirectory(FolderPathName);
                        return true;
                    }
                        return false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message);
                }
                
            }
            return false;
        }
    }
}